package tablero;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import grafos.AbstractGraph;
import grafos.UndirectedGraph;
import constants.Constants;

public class Tablero {
	private AbstractGraph<Square> mapaTablero;
	private List<Square> listaCasillas;
	
	public Tablero(){
		mapaTablero=new UndirectedGraph<Square>();
		listaCasillas= new ArrayList<Square>();
		//ReadFromXML reader = new ReadFromXML(Constants.MAP_FILE);
	}
	
	public List<Square> getSquareList(){
		return listaCasillas;
	}
	
	public boolean addSquare(Square casilla){
		if(mapaTablero.addVert(casilla)){
			listaCasillas.add(casilla);
			return true;
		}
		else return false;
	}
	
	public boolean removeSquare(Square casilla){
		if(mapaTablero.removeVert(casilla)){
			listaCasillas.remove(casilla);
			return true;
		}
		else return false;
	}
	
	public boolean isSquare(Square casilla){
		if(mapaTablero.isVert(casilla)) return true;
		else return false;
	}
	
	public boolean connectSquare(Square c1,Square c2){
		if(mapaTablero.addEdge(c1, c2))return true;
		else return false;
	}
	
	public boolean connectSquare(Square c1,List<Square> list){
		boolean ok=true;
		for(Square casilla : list) 
			if(!mapaTablero.addEdge(c1, casilla)) ok=false;
		return ok;
	}
	
	public boolean disconnectSquare(Square c1,Square c2){
		if(mapaTablero.removeEdge(c1, c2))return true;
		else return false;
	}
	
	public Set<Square> squaresConnectedTo(Square c1){
		Set<Square> set= new TreeSet<Square>(mapaTablero.getIncomingVertexOf(c1));
		return set;
	}
	
	private class ReadFromXML extends DefaultHandler{
		
		private SAXParserFactory spfac;
		SAXParser sp;
		Square sqrTmp;
		String tmpValue;
		Square origen;
		ArrayList<Square> listaCasillasDst;
		
		public ReadFromXML(String fichero){
			spfac = SAXParserFactory.newInstance();
			try {
				sp = spfac.newSAXParser();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				sp.parse(fichero, this);
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void startElement(String uri, String localName,
                String elementName, Attributes attributes) throws SAXException {
			if(elementName.equalsIgnoreCase("square")){
				sqrTmp = new Square();
			}
		}
		
		public void endElement(String s, String s1, String element) throws SAXException {
			if(element.equalsIgnoreCase("square")){
				addSquare(sqrTmp);
			}
			if(element.equalsIgnoreCase("name")){
				sqrTmp.setSquareName(tmpValue);
			}
			if(element.equalsIgnoreCase("number")){
				sqrTmp.setSquareNumber(Integer.parseInt(tmpValue));
			}
			if(element.equalsIgnoreCase("origin")){
				
			}
		}
		
		public void characters(char[] ac, int i, int j) throws SAXException {
			tmpValue = new String(ac, i, j);
		}	
	}//END PRIVATE CLASS
	
}//END CLASS
